var id=getvl("id");
//var nuber=getvl("nuber");
//var uId=getvl("uId");
//var dId=getvl("dId");
//var orderid=getvl("orderid"); //订单
var ids=getvl("ids");

$(function(){
	  if(ids!=""){
	   	   xiugaidingdan();
   var tui='<input type="button" class="p_button" onclick="xiugai('+ids+')" value="修改退款">';
	  	$("#xiugais").html(tui);
	  }else{
	  	
   var tui='<input type="button" class="p_button" onclick="refundOrder()" value="提交退款">';
	  	$("#xiugais").html(tui);
	  }
})








function refundOrder(){
	    	 var types=$("#types").html();
	    	 var leixing=$("#leixing").html();
	    	  var leixings=0;
	    if(leixing=="退款退货"){
	    	leixings=1;
	    }else if(leixing=="仅退款"){
	    	leixings=2;
	    }
	    
	    	if(id!='' && leixings!=0 && types!="请选择" && uuuuid!=""){
	  
	      var xijie="";
	    $(".xijietu").each(function(t,y){
	    	  xijie+=$(this).val()+"***";
	    });
	     var xijietupian=xijie.substring(0,xijie.length-3);
	
	    var refundmoney=$("#refundmoney").val();
	    var refundRemark=$("#refundRemark").val();
		 $.ajax({
		url: urlcore + '/api/refundOrder/add2', /*/api/refundOrder/updateRefundOrderStatus"*/
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			   orderId:id,
			   refundType:leixings,
			   userId:uuuuid,
			   refundReason:types,
			   refundMoney:refundmoney,
			   refundRemark:refundRemark,
			   refundImg:xijietupian
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
		     // alert("等待卖家审批中");
		      //dingdandaishouhuo();
		     window.location.href = document.referrer;
		     }
		},error: function() {
			alert("error");
		}
	});
	    
	   	}else{
	   		alert("请将信息填写完整");
	   	}
}



 function xiugaidingdan(){
	 		 	  $.ajax({
      url: urlcore+"/api/refundOrder/selectOne?id="+ids,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					var da=data.data;
				var leixing="请选择";
				if(da.refundType==1){
					leixing="退款退货";
				}else if(da.refundType==2){
					leixing="仅退款";
				}
				$("#leixing").html(leixing);
				$("#types").html(da.refundReason);
				$("#refundmoney").val(da.refundMoney);
				$("#refundRemark").val(da.refundRemark);
					$("#tupain").html("");
					var imgs=da.refundImg;
               var imgStr1="";
               if(imgs!=null){
              var imgarr1 = imgs.split("***");
              $.each(imgarr1,function(index,value){
              //	alert(value);
              imgStr1 +='<li><input type="hidden" class="xijietu" value="'+value+'" id="xijietu"><img src="'+value+'"><span data-name=delete></span></li>';
              });
               }
           imgStr1+='<li>'+
                '<img src="../images/photo.png">'+
                '<input  type="file" onchange="previewImage2(this)">'+
                '</li>';
                $("#tupain").append(imgStr1);
                $("#xiugais").html("");
                var  xiugais='<input type="button" class="p_button" onclick="xiugai('+da.id+')"  value="修改退款">';
                $("#xiugais").append(xiugais);
					$(function(){
						
    $(".mm_imgfile input[type=file]").change(function(){
        var fs = this.files,$t  = $(this);
        $(fs).each(function(){
              var reader = new FileReader();
                reader.readAsDataURL(this);
                reader.onload = function(e) {
                    $t.parent().parent().prepend($('<li><input type="hidden"  class="xijietu"  id="xijietu"><img src="'+this.result+'"><span data-name=delete></span></li>'));
                    deleteImg();
                }
        });
    });
    deleteImg();
    function deleteImg(){
        var $span = $("*[data-name=delete]");
        $span.unbind("click");
        $span.click(function(){
            $(this).parent().remove();
        });
    }
    
					})
					
				}
	
			},
			error: function() {
				alert("error");
			}
		});
	 
	 }




function xiugai(te){
	var xijie="";
	    $(".xijietu").each(function(t,y){
	    	  xijie+=$(this).val()+"***";
	    	  
	    });
	     var types=$("#types").html();
	     var xijietupian=xijie.substring(0,xijie.length-3);
	
	    var refundmoney=$("#refundmoney").val();
	    var refundRemark=$("#refundRemark").val();
	        var leixing=$("#leixing").html();
	    var leixings=0;
	    if(leixing=="退款退货"){
	    	leixings=1;
	    }else if(leixing=="仅退款"){
	    	leixings=2;
	    }
	    	if(ids!='' && leixings!=0 && types!="请选择" && uuuuid!=""){
	  
		 $.ajax({
		url: urlcore + '/api/refundOrder/update', /*/api/refundOrder/updateRefundOrderStatus"*/
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			   id:te,
			   refundType:leixings,
			   userId:uuuuid,
			   refundReason:types,
			   refundmoney:refundmoney,
			   refundRemark:refundRemark,
			   refundImg:xijietupian
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
		     window.location.href = document.referrer;
		     }
		},error: function() {
			alert("error");
		}
	});
	    
	   }
}



$(function(){

     $.ajax({
			url: urlcore + "/api/order/selectOrderid?id="+id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success:function(data){
				if(data.success == true) {
					$("#refundmoney").val(data.data.payMoney);
				}
			},
			error:function() {
				alert("error");
			}
		});

	
	
});
